﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace Ornithopter.FlashDiskHelper
{
    public abstract class ItemViewControlBase : UserControl,IDisposable
    {
        public LogicalDisk LogicalDisk { get; private set; }

        public ItemViewControlBase(LogicalDisk logicalDisk)
        {
            LogicalDisk = logicalDisk;
            this.Opacity = 0.0;
            this.Loaded += new RoutedEventHandler(ItemViewControlBase_Loaded);
        }

        void ItemViewControlBase_Loaded(object sender, RoutedEventArgs e)
        {
            ShowUp();
        }

        private void ShowUp()
        {
            this.Opacity = 1.0;
            Animations.Popup(this).Begin();
        }

        public void Close(Action callback)
        {
            this.Opacity = 1.0;
            var sb = Animations.Popout(this);
            EventHandler completed = null;
            completed = (s, e) =>
            {
                sb.Completed -= completed;
                if (callback != null)
                    callback();
            };
            sb.Completed += completed;
            sb.Begin();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            InitiateUI();
            if(EjectButton != null)
                EjectButton.Click += new RoutedEventHandler(EjectButton_Click);
            if(OpenButton != null)
                OpenButton.Click += new RoutedEventHandler(OpenButton_Click);
        }

        void OpenButton_Click(object sender, RoutedEventArgs e)
        {
            OpenExplorer(LogicalDisk.Properties.Caption);
        }

        void EjectButton_Click(object sender, RoutedEventArgs e)
        {
            LogicalDisk.OwnerDevice.Eject();
        }

        protected override void OnMouseEnter(MouseEventArgs e)
        {
            base.OnMouseEnter(e);
            VisualStateManager.GoToState(this, "Expanded", true);
        }

        protected override void OnMouseLeave(MouseEventArgs e)
        {
            base.OnMouseLeave(e);
            VisualStateManager.GoToState(this, "Normal", true);
        }

        public abstract ButtonBase EjectButton { get; }
        public abstract ButtonBase OpenButton { get; }

        protected abstract void InitiateUI();

        private static void OpenExplorer(string path)
        {
            string winDirpath = @Environment.GetEnvironmentVariable("Windir").ToString();
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(winDirpath + @"\explorer.exe");
            if (System.IO.Directory.Exists(path))
            {
                psi.Arguments = "/e," + path;
            }
            else
            {
                psi.Arguments = "/e,/select," + path;
            }
            System.Diagnostics.Process.Start(psi);
        }

        public void Dispose()
        {
            if (LogicalDisk != null)
                LogicalDisk.Dispose();
        }
    }
}
